package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //new Person("张三");
        Class cls = Class.forName("reflect.Person");
        /*
            Class的构造器方法：
        */
        Constructor con = cls.getConstructor(String.class);//不传参时，获取的就是无参构造器，Person()
        //new Person("张三")
        Object obj = con.newInstance("张三");
        System.out.println(obj);

        //Person(String,int)
        Constructor con2 = cls.getConstructor(String.class,int.class);
        Object obj2 = con2.newInstance("李四",20);
        System.out.println(obj2);
    }
}
